package com.tqw.client.controller;

import com.tqw.client.service.ClientFeignService;
import com.tqw.client.vo.ReceivingVO;
import com.tqw.entity.Result;
import com.tqw.entity.StatusCode;
import com.tqw.pojo.Area;
import com.tqw.vo.ShippingVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.tqw.entity.StatusCode.ERROR;
import static com.tqw.entity.StatusCode.OK;

/**
 * 调用Feign接口得controller类
 * @author Will
 */
@RestController
@RequestMapping("/client/feign")
public class ClientFeignController {

    @Autowired
    ClientFeignService clientFeignService;

    /**
     * 从tqw-shipping-api的feign接口中查询地址列表Result<List<ShippingVO>>
     *     得到其中的 ShippingVO 封装成 ReceivingVO 对象
     *     得到 List<ReceivingVO> 对象
     *     返回 Result<List<ReceivingVO>> 对象
     * @param
     * @return
     */
    @GetMapping("/shippingAddressList")
    Result<List<ReceivingVO>> shippingAddressList(@RequestHeader("Authorization") String token){
        List<ReceivingVO> receivingVOList = clientFeignService.shippingAddressList(token);
        if(receivingVOList == null){
            return new Result<>(false, ERROR, "查询失败");
        }
        return new Result<>(true, OK, "查询成功", receivingVOList);
    }

    /**
     * 个人中心地址 编辑对话框数据回显
     * @param
     * @param addressId
     * @return
     */
    @GetMapping("/shippingAddressOne")
    Result<ShippingVO> shippingAddressOne(@RequestHeader("Authorization") String token,@RequestParam("addressId")  Integer addressId){
        ReceivingVO receivingVO = clientFeignService.shippingAddressOne(token,addressId);
        if(receivingVO == null){
            return new Result<>(false, ERROR, "回显失败");
        }
        return new Result<>(true, OK, "回显成功", receivingVO);
    }

    /**
     * 级联获取省市街道
     * @return
     */
    @GetMapping("/shippingArea")
    Result<List<Area>> shippingArea(){
        List<Area> areaList = clientFeignService.shippingArea();
        if(areaList == null){
            return new Result<>(false, ERROR, "级联查询失败");
        }
        return new Result<>(true, OK, "级联查询成功", areaList);
    }

    /**
     * 表单提交更新收货地址
     * @param shippingVO
     * @return
     */
    @PutMapping("/shippingReceivingUpdate")
    Result shippingReceivingUpdate(@RequestHeader("Authorization") String token,@RequestBody ShippingVO shippingVO){
        Result result = clientFeignService.shippingReceivingUpdate(token,shippingVO);
        if(result == null){
            return new Result<>(false, ERROR, "更新失败");
        }
        return new Result<>(true, OK, "更新成功",result);
    }

    /**
     * 删除收货地址信息
     * RestFul风格传参
     * @return
     */
    @DeleteMapping("/shippingReceivingDelete/{addressId}")
    Result shippingReceivingDelete(@RequestHeader("Authorization") String token,@PathVariable Integer addressId){
        Result result = clientFeignService.shippingReceivingDelete(token,addressId);
        if(result == null){
            return new Result<>(false, ERROR, "删除失败");
        }
        if(result.getCode()== StatusCode.ISDEFAULY){
            return new Result<>(false, result.getCode(), result.getMessage());
        }
        return new Result<>(true, OK, "删除成功");
    }

    /**
     * 新增收货地址信息
     * @param shippingVO
     * @return
     */
    @PostMapping("/shippingReceivingInsert")
    Result shippingReceivingInsert(@RequestHeader("Authorization") String token,@RequestBody ShippingVO shippingVO){
        Result result = clientFeignService.shippingReceivingInsert(token,shippingVO);
        if(result == null){
            return new Result<>(false, ERROR, "新增失败");
        }
        if(result.getCode()== ERROR){
            return new Result<>(false, result.getCode(), result.getMessage());
        }
        return new Result<>(true, OK, "新增成功");
    }

}
